/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.bocai.dahuatech.ui.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.OrderListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.ui.activity.MyOrderActivity;
import com.bocai.dahuatech.ui.pressent.MyorderPressent;
import com.bocai.dahuatech.ui.view.MyListView;

import java.util.List;

import de.greenrobot.event.EventBus;

public class MyOrderAdapterHolder extends RecyclerView.Adapter<MyOrderAdapterHolder.ViewHolder> {
    private View view;
    private AlertDialog dialog;
    OnItemClickListener mItemClickListener;
    private Context context;
    private List<OrderListBean.ContentEntity> datas;
    MyorderPressent pressent;
    String mQuery;
    public MyOrderAdapterHolder(Context context, List<OrderListBean.ContentEntity> datas,MyorderPressent pressent,String mQuery) {
        this.context = context;
        this.datas=datas;
        this.pressent=pressent;
        this.mQuery=mQuery;
    }
    public void addDatas(List<OrderListBean.ContentEntity> list){
        datas.addAll(list);
        this.notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
        final View sView = mInflater.inflate(R.layout.layout_myorder, parent, false);
        return new ViewHolder(sView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.tv_ordernumber.setText(context.getResources().getString(R.string.ordernumbers)+datas.get(position).getBillNo());
        if(datas.get(position).getStatus().equals(context.getResources().getString(R.string.ordertypeready))){
            holder.ll_dismissorder.setVisibility(View.VISIBLE);
            holder.tv_dismissorder.setText(R.string.dismissorder);
        }else if(datas.get(position).getStatus().equals(context.getResources().getString(R.string.ordertypealready)))
        {
            holder.ll_dismissorder.setVisibility(View.VISIBLE);
            holder.tv_dismissorder.setText(R.string.confirmreceipt);
        }else if(datas.get(position).getStatus().equals(context.getResources().getString(R.string.ordertypecomplete)))
        {
            holder.ll_dismissorder.setVisibility(View.GONE);
//            holder.tv_dismissorder.setText("删除订单");
        }
        else{
            holder.ll_dismissorder.setVisibility(View.GONE);
        }
        holder.tv_num_price.setText(context.getString(R.string.gong) + datas.get(position).getTotalQty() + context.getString(R.string.productcountandmoney)+ datas.get(position).getTotalAmount());//共2件商品 合计 ：￥310
        holder.tv_order_state.setText(datas.get(position).getStatus());
        holder.mlvMyOrder.setAdapter(new MyOrderAdapter(context, datas.get(position).getProductList()));
        holder.mlvMyOrder.setOnItemClickListener(new MyOnItemClickListener(position));
        holder.ll_dismissorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(context.getResources().getString(R.string.dismissorder).equals(holder.tv_dismissorder.getText()))
                {

                    view= LayoutInflater.from(context).inflate(R.layout.dialog_dismiss_order,null);
                    TextView tvgoShopcard= (TextView) view.findViewById(R.id.tv_deleteorder_goshopcard);
                    TextView tvdelete= (TextView) view.findViewById(R.id.tv_deleteorder_delete);
                    view.setBackgroundResource(R.drawable.dialog_shape);
                    dialog=new AlertDialog
                            .Builder(context)
                            .setView(view)
                            .show();
                    //去购物车修改
                    tvgoShopcard.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            pressent.setIsShow(false);
                            EventBus.getDefault().post(new MainEvent("orderset", datas.get(position).getFID(),mQuery));
                            dialog.cancel();
                        }
                    });
                    //取消订单
                    tvdelete.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v) {
                            pressent.setIsShow(false);
                            EventBus.getDefault().post(new MainEvent("ordercanel",datas.get(position).getFID(), mQuery));
                            dialog.cancel();
                        }
                    });
                }else if (context.getResources().getString(R.string.confirmreceipt).equals(holder.tv_dismissorder.getText())) {
                    pressent.setIsShow(false);
                    EventBus.getDefault().post(new MainEvent("receivegoods", datas.get(position).getFID(),mQuery));
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    class MyOnItemClickListener implements AdapterView.OnItemClickListener{
        int position;
        public  MyOnItemClickListener(int position){
            this.position=position;
        }
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int positions, long id) {
            Intent intent=new Intent(context, MyOrderActivity.class);
            intent.putExtra("fid",datas.get(position).getFID());
            intent.putExtra("totalprice",context.getString(R.string.money)+datas.get(position).getTotalQty()+context.getString(R.string.productcountandmoney)+datas.get(position).getTotalAmount());
            intent.putExtra("state",datas.get(position).getStatus());
            context.startActivity(intent);
        }
    }
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView tv_ordernumber,tv_order_state,tv_num_price,tv_dismissorder;
        MyListView mlvMyOrder;
        LinearLayout ll_dismissorder;
        public ViewHolder(View view) {
            super(view);
            mlvMyOrder=(MyListView)view.findViewById(R.id.mlv_myorder);
            tv_ordernumber= (TextView) view.findViewById(R.id.tv_ordernumber);
            tv_order_state= (TextView) view.findViewById(R.id.tv_order_state);
            tv_num_price= (TextView) view.findViewById(R.id.tv_num_price);
            tv_dismissorder= (TextView) view.findViewById(R.id.tv_dismissorder);
            ll_dismissorder=(LinearLayout)view.findViewById(R.id.ll_dismissorder);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
//                mItemClickListener.onItemClick(v, getPosition());
                mItemClickListener.onItemClick(v, getLayoutPosition());
            }
        }

    }

}
